package com.tingyuge.base.thread.tools;

import lombok.Data;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.IntStream;

/**
 * @author xiaodao
 * @version 1.0
 * @date 2021/3/20
 */
public class ReadWrite1 {
    private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    public void  method(){
        readWriteLock.readLock().lock();
        try {
            Thread.sleep(1000);
            System.out.println("method");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            readWriteLock.readLock().unlock();
        }
    }

    public static void main(String[] args) {
        ReadWrite1 readWrite1 = new ReadWrite1();

        IntStream.range(0,10).forEach(i->{
            new Thread(()->readWrite1.method()).start();
        });
    }
}
